﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SedionFormDesigner.Infrastructure;
using System.ComponentModel.Design;
using System.Windows.Forms;
using SedionFormDesigner.Designer;
using System.Windows.Forms.Design;
using SedionFormDesigner.SedienFormDesigner.Infrastructure;
using System.Xml.Linq;

namespace SedionFormDesigner
{
    public class SedionDesignerManager
    {
        private Int32 hostCount;

        private ToolboxPane toolbox;
        public ToolboxPane Toolbox { get { return toolbox; } }

        private PropertyGrid propertyGrid;
        public PropertyGrid PropertyGrid { get { return propertyGrid; } }

        private static List<SedionDesignerHost> hosts;
        private List<SedionDesignerLoader> loaders;

        //private SedionDesignerLoader loader;
        //private KeystrokeMessageFilter filter;
        private ServiceContainer hosingServiceContainer;

        public SedionDesignerManager()
        {
            toolbox = new ToolboxPane();
            propertyGrid = new PropertyGrid();
            propertyGrid.Dock = DockStyle.Fill;

            hosts = new List<SedionDesignerHost>();
            loaders = new List<SedionDesignerLoader>();

            removeHosts = new List<SedionDesignerHost>();

            hosingServiceContainer = new ServiceContainer();
            hosingServiceContainer.AddService(typeof(ToolboxPane), toolbox);
            hosingServiceContainer.AddService(typeof(PropertyGrid), propertyGrid);
        }

        private Boolean changed = false;
        public Boolean Changed { get { return checkChanged(); } }

        public Control NewDesigner()
        {
            return InitForm(null);
        }

        public Control InitForm(XElement xml)
        {
            hostCount++;
            SedionDesignerLoader designerLoader = new SedionDesignerLoader(xml);
            loaders.Add(designerLoader);

            var host = new SedionDesignerHost(hosingServiceContainer, hostCount);
            hosts.Add(host);

            toolbox.Host = host;
            propertyGrid.PropertyTabs.AddTabType(typeof(EventsTab));
            host.LoadDocument(designerLoader);
            //this.loader = designerLoader;

            toolbox.Host = host;
            host.View.Dock = DockStyle.Fill;
            return host.View;
        }

        private static List<Control> forms;
        public static List<Control> Forms
        {
            get
            {
                forms = new List<Control>();
                foreach (var item in hosts)
                {
                    forms.Add(getForm(item.View));
                }
                return forms;
            }
        }

        private static Control getForm(Control parentCtrl)
        {
            if (parentCtrl.GetType() == typeof(System.Windows.Forms.Form))
                return parentCtrl;
            else
            {
                Control result = parentCtrl;
                if (parentCtrl.Controls.Count > 0)
                {
                    foreach (Control ctrl in parentCtrl.Controls)
                    {
                        result = getForm(ctrl);
                    }
                }
                return result;
            }
        }

        public List<SedionDesignerHost> Hosts
        {
            get
            {
                return hosts;
            }
        }

        public void DelelteDesigner(Int32 index)
        {
            hosts[index].Dispose();
            hosts.RemoveAt(index);

            loaders[index].Dispose();
            loaders.RemoveAt(index);
        }

        private Boolean checkChanged()
        {
            var temp = false;
            for (int i = 0; i < loaders.Count; i++)
            {
                if (!loaders[i].Saved)
                {
                    temp = true;
                    break;
                }
            }
            changed = temp;
            return changed;
        }

        public XElement GetDesignerXML(String formName)
        {
            XElement xml = new XElement("Form");
            xml.SetAttributeValue("FormName", formName);

            for (int i = 0; i < hosts.Count; i++)
            {
                loaders[i].Save();
                xml.Add(hosts[i].Save(i + 1));
            }
            return xml;
        }


        private List<SedionDesignerHost> removeHosts;

        public List<SedionDesignerHost> RemoveHosts
        {
            get
            {
                return removeHosts;
            }
            set
            {
                removeHosts = value;
            }
        }
    }
}
